package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R add(DishDto dishDto) {
        //1.校验参数
        if (dishDto==null){
            return R.error("参数异常");
        }
        //2.保存菜品数据到菜品表
        //2.1创建一个菜品对象
        Dish dish = new Dish();
        //2.2使用spring工具类来复制对象
        BeanUtils.copyProperties(dishDto,dish);
        dishMapper.insert(dish);
        //3.循环保存菜品口味到菜品口味表中
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            //补全数据,补全菜品id
            flavor.setDishId(dish.getId());
            //循环保存口味到口味表
            dishFlavorMapper.insert(flavor);
        }
        return R.success("添加成功");
    }

    //菜品分页查询
    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        Page<Dish> p = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            wrapper.like(Dish::getName,name);
        }
        Page<Dish> dishPage = dishMapper.selectPage(p, wrapper);

        List<Dish> records = dishPage.getRecords();
        String toJSONString = JSON.toJSONString(records);
        List<DishDto> dishDtos = JSON.parseArray(toJSONString, DishDto.class);
        dishDtos.forEach(c->{
            Category category = categoryMapper.selectById(c.getCategoryId());
            c.setCategoryName(category.getName());
        });
        String jsonString = JSON.toJSONString(dishPage);
        Page pageInfo = JSON.parseObject(jsonString, Page.class);
        pageInfo.setRecords(dishDtos);
        return R.success(pageInfo);
    }

    //根据ID查询菜品信息和菜品口味
    @Override
    public R findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorMapper.selectList(wrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);
        return R.success(dishDto);
    }
    //修改
    @Override
    public R update(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dishMapper.updateById(dish);
        LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorMapper.insert(flavor);
        }
        return R.success("修改成功");
    }
    //批量删除
    @Override
    public R deleteIds(Long[] ids) {
        LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();

        for (Long id : ids) {
            //根据菜品Id先删除菜品口味
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorMapper.delete(wrapper);
       }
        //然后删除菜品
        dishMapper.deleteBatchIds(Arrays.asList(ids));
        return R.success("删除成功");
    }
    //批量起售
    //批量停售
    @Override
    public R modyfiStatus(Long[] ids,Integer id) {
//        for (Long id : ids) {
//            Dish dish = dishMapper.selectById(id);
//            dish.setStatus(1);
//            dishMapper.updateById(dish);
//        }
        Dish dish = new Dish();
        dish.setStatus(id);
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Dish::getStatus,id).in(Dish::getId,ids);
        dishMapper.update(dish,wrapper);
        return R.success("批量修改成功");
    }
    //根据分类id查询出菜品的集合信息
    @Override
    public R findList(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //把dish 转换成dishDto
        String jsonString = JSON.toJSONString(dishList);
        List<DishDto> dishDtos = JSON.parseArray(jsonString, DishDto.class);
        //循环给菜添加口味信息
        for (DishDto dishDto : dishDtos) {
            //根据菜品的id 查询菜的口味
            LambdaQueryWrapper<DishFlavor> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(DishFlavor::getDishId,dishDto.getId());
            List<DishFlavor> flavors = dishFlavorMapper.selectList(wrapper2);
            dishDto.setFlavors(flavors);
        }
        return R.success(dishDtos);
    }

}
